package com.bifrii.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import com.bifrii.transferObjects.CategoryCommunity;

public class JDBCCategoryCommunityDAO implements ICategoryCommunityDAO {

	@Override
	public void insert(Integer idCategory, CategoryCommunity c) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		
        String sql = "INSERT INTO CommunityCategory ( idCategory, name) VALUES (?, ?) ";
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);

            stmt.setInt(1, idCategory);
            stmt.setString(2, c.getName());
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }


	}

	@Override
	public List<CategoryCommunity> selectAll() {
		Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List<CategoryCommunity> searchResults = new LinkedList<CategoryCommunity>();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM CommunityCategory order by idCategory desc";
            stmt = conn.prepareStatement(sql); //conn es una conexi�n v�lida
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
                CategoryCommunity temp = new CategoryCommunity();
                temp.setIdCategory(result.getInt("idCategory"));
                temp.setName(result.getString("name"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
	}

	@Override
	public CategoryCommunity select(Integer idCategory) {
		Connection conn = ConnectionManager.getInstance().checkOut();
		PreparedStatement stmt = null;
        ResultSet result = null;
        CategoryCommunity c = null;
        String sql = "SELECT * FROM CommunityCategory WHERE (idCategory = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setInt(1, idCategory);
            result = stmt.executeQuery();

            result.next();
            c = new CategoryCommunity();
            c.setIdCategory(result.getInt("idCategory"));
            c.setName(result.getString("name"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return c;
	}

}
